package g2.routemaster.gui.contents;

import g2.routemaster.gui.BoardSingleton;
import g2.routemaster.gui.GManager;
import g2.routemaster.model.GameManager;
import g2.routemaster.model.Player;

import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;

public class GameBoardComposite extends Composite {
   private Composite contents;
   private BoardCardsGroup boardCardsGroup;
   private OfficialsGroup officialsGroup;
   private BoardGroup boardGroup;
   private Composite playersComposite;

   public GameBoardComposite(Composite parent, int style) {
      super(parent, style);
   }

   public void create(){
      // add to singleton
      BoardSingleton.instance().setGameBoard(this);

      contents = new Composite(this, SWT.NONE);
      GridData gd;
      GridLayout layout = new GridLayout();
      layout.numColumns = 1;
      layout.makeColumnsEqualWidth = true;
      layout.horizontalSpacing = 0;
      layout.verticalSpacing = 0;
      layout.marginTop = 0;
      contents.setLayout(layout);
      contents.setLayoutData(new GridData(GridData.FILL_BOTH));

      Composite compositeBoard = new Composite(contents, SWT.NONE);
      layout = new GridLayout();
      layout.numColumns = 2;
      layout.horizontalSpacing = 0;
      layout.verticalSpacing = 0;
      compositeBoard.setLayout(layout);
      gd = new GridData(GridData.FILL_BOTH);
      gd.horizontalSpan = 3;
      compositeBoard.setLayoutData(gd);

      officialsGroup = new OfficialsGroup(compositeBoard, SWT.NONE);
      officialsGroup.create();
      officialsGroup.setLayout(new FillLayout());

      boardGroup = new BoardGroup(compositeBoard, SWT.NONE);
      boardGroup.create();
      layout = new GridLayout();
      layout.horizontalSpacing = 0;
      layout.verticalSpacing = 0;
      layout.marginWidth = 0;
      boardGroup.setLayout(layout);
      gd = new GridData(GridData.FILL_BOTH);
      gd.verticalSpan = 2;
      boardGroup.setLayoutData(gd);

      boardCardsGroup = new BoardCardsGroup(compositeBoard, SWT.NONE);
      boardCardsGroup.create();
      layout = new GridLayout();
      layout.horizontalSpacing = 0;
      layout.verticalSpacing = 0;
      layout.marginWidth = 0;
      boardCardsGroup.setLayout(layout);
      boardCardsGroup.setLayoutData(new GridData(GridData.BEGINNING));

      playersComposite = new Composite(compositeBoard, SWT.NONE);
      layout = new GridLayout();
      layout.numColumns = 4;
      layout.horizontalSpacing = 0;
      layout.verticalSpacing = 0;
      playersComposite.setLayout(layout);
      gd = new GridData(GridData.HORIZONTAL_ALIGN_CENTER);
      gd.horizontalSpan = 2;
      playersComposite.setLayoutData(gd);

      updatePlayers();
      updateBoardCards();
      updateOfficials();
      updateBoard();
   }

   public void updateOfficials(){
      officialsGroup.updateData();
   }

   public void updateBoardCards(){
      boardCardsGroup.updateData();
   }
   
   public void updateBoard(){
      boardGroup.updateData();
   }

   void initSeatPlaces(long count){
      for(int i=0; i<count; i++){
         PlayerGroup playerGroup = new PlayerGroup(playersComposite, SWT.NONE);
         playerGroup.create();
         playerGroup.setLayout(new GridLayout());
         playerGroup.setLayoutData(new GridData(GridData.FILL_VERTICAL));
      }
   }

   public void updatePlayers(){
      GameManager gm = GManager.instance().getGameManager();

      if(playersComposite.getChildren().length == 0){
         initSeatPlaces(gm.getPlayerCount());
      }

      // update hot player
      Player player = gm.getFirstPlayer();
      for(Control control : playersComposite.getChildren()){
         PlayerGroup group = (PlayerGroup) control;
         group.updateData(player);
         player = gm.getNextPlayer(player);
      }
   }
}
